import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:extended_tabs/extended_tabs.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/base/qkd_button.dart';
import 'package:sports_new/widgets/base/qkd_indicator.dart';
import 'package:sports_new/widgets/focus_button.dart';
import '../utils/routes.dart';
import '../widgets/base/qkd_text.dart';
import '../widgets/ball_header.dart';
import 'colours.dart';

class Styles {
  static AppBar appBar(
      {List<Widget> actions = const [],
      IconThemeData? iconTheme,
      Color leadingColor = Colors.black,
      TextStyle? titleTextStyle,
      Widget? title,
      bool primary = true,
      SystemUiOverlayStyle? systemUiOverlayStyle,
      ScrollPhysics? physics,
      Color? foregroundColor,
      Color? backgroundColor,
      double? titleSpacing,
      double? toolbarHeight,
      bool centerTitle = true,
      bool automaticallyImplyLeading = false,
      bool showLeading = true,
      double? elevation,
      VoidCallback? onTapLeading}) {
    return AppBar(
        centerTitle: centerTitle,
        actions: actions,
        primary: primary,
        systemOverlayStyle: systemUiOverlayStyle,
        toolbarHeight: toolbarHeight,
        foregroundColor: foregroundColor,
        backgroundColor: backgroundColor,
        title: title,
        elevation: elevation,
        automaticallyImplyLeading: automaticallyImplyLeading,
        titleSpacing: titleSpacing,
        titleTextStyle: titleTextStyle,
        iconTheme: iconTheme,
        leadingWidth: 40,
        leading: Get.currentRoute != Routes.navigation && showLeading
            ? GestureDetector(
          onTap: Get.back,
          child: Container(
            height: Get.height,
            padding: const EdgeInsets.only(left: 16),
            child: Image.asset(
              Utils.getImgPath('arrow_back.png'),
              width: 24,
              height: 24,
              color: leadingColor
            ),
          ),
        ):null);
  }

  static ExtendedTabBar tabbar({
    Key? key,
    required List<Widget> tabs,
    TabController? controller,
    ValueChanged<int>? onTap,
    double fontSize = 16,
    double? unselectedFontSize,
    ScrollPhysics? physics,
    Decoration? indicator,
    double indicatorBottom = 3,
    EdgeInsetsGeometry labelPadding = const EdgeInsets.symmetric(horizontal: 4),
    bool scrollable = false,
    labelColor = Colours.text,
    unselectedLabelColor = Colours.grey99,
    fontWeight = FontWeight.w600,
    unselectedFontWeight = FontWeight.w500,
    MainAxisAlignment? mainAxisAlignment
  }) {
    return ExtendedTabBar(
      key: key,
      tabs: tabs,
      physics: physics,
      labelPadding: labelPadding,
      controller: controller,
      indicator: indicator ??
        QkdIndicator(
          borderSide: const BorderSide(color: Colours.main, width: 3),
          indicatorWidth: 16,
          indicatorBottom: indicatorBottom),
      isScrollable: scrollable,
      labelColor: labelColor,
      indicatorSize: TabBarIndicatorSize.tab,
      unselectedLabelColor: unselectedLabelColor,
      unselectedLabelStyle:
          TextStyle(fontSize: unselectedFontSize ?? fontSize, fontWeight: unselectedFontWeight),
      labelStyle: TextStyle(fontSize: fontSize, fontWeight: fontWeight),
      onTap: onTap,
      mainAxisAlignment: mainAxisAlignment,
    );
  }

  static StrutStyle centerStyle(
      {required double? fontSize, FontWeight? fontWeight}) {
    return StrutStyle(
        fontSize: fontSize,
        height: 1.172,
        forceStrutHeight: true,
        fontWeight: fontWeight ?? FontWeight.w400);
  }

  static TextStyle normalText({required double fontSize}) {
    return TextStyle(
        fontSize: fontSize, fontWeight: FontWeight.w400, color: Colours.text);
  }

  static TextStyle mediumText({required double fontSize}) {
    return TextStyle(
        fontSize: fontSize, fontWeight: FontWeight.w500, color: Colours.text);
  }

  static TextStyle boldText({required double fontSize}) {
    return TextStyle(
        fontSize: fontSize, fontWeight: FontWeight.w600, color: Colours.text);
  }

  static TextStyle normalSubText({required double fontSize}) {
    return TextStyle(
        fontSize: fontSize,
        fontWeight: FontWeight.w400,
        color: Colours.grey66);
  }

  static TextStyle mediumSubText({required double fontSize}) {
    return TextStyle(
        fontSize: fontSize,
        fontWeight: FontWeight.w500,
        color: Colours.grey66);
  }

  static TextStyle boldSubText({required double fontSize}) {
    return TextStyle(
        fontSize: fontSize,
        fontWeight: FontWeight.w600,
        color: Colours.grey66);
  }

  static Widget placeholderIcon({double? width, double? height}) {
    return Container(
      color: Colours.greyD9,
      width: width,
      height: height,
    );
  }

  static Widget card(
      {Widget? child,
      AlignmentGeometry? alignment,
      EdgeInsets? padding,
      Color? color,
      // this.foregroundDecoration,
      double? width,
      double? height,
      BoxConstraints? constraints,
      // Clip? clipclipBehavior,
      EdgeInsets? margin,
      double? radius = 8,
      bool shadow = true}) {
    return Container(
      margin: margin,
      padding: padding,
      alignment: alignment,
      width: width,
      height: height,
      clipBehavior: Clip.hardEdge,
      constraints: constraints,
      decoration: BoxDecoration(
        color: color ?? Colours.white,
        // shape: BoxShape.circle,
        borderRadius: radius == null ? null : BorderRadius.circular(radius),
        boxShadow: shadow
            ? [
                const BoxShadow(
                  color: Color(0x0C0F499F),
                  offset: Offset(0, 2),
                  blurRadius: 12,
                ),
              ]
            : null,
      ),
      child: child,
    );
  }

  static backButton({Color color = Colours.white}) {
    return IconButton(
      onPressed: () {
        Get.back();
      },
      icon: Image.asset(
        Utils.getImgPath('arrow_back.png'),
        color: color,
        width: 24,
        height: 24,
      ),
    );
  }

  static matchListGroupHeader(DateTime date) {
    String? dateStr = DateUtils.isSameDay(date, DateTime.now()) ? '今天 ' : "";
    dateStr +=
      '${DateTimeUtils.formatDateTime(date, 'MM/dd')} ${DateFormat.E('zh_cn').format(date)}';
      if(date.year == 1999) dateStr = "已结束";
    return Container(
      height: 28.0,
      color: Colours.greyF2F3F7,
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      alignment: Alignment.center,
      child: QkdText(
        dateStr,
        fontSize: 12,
      ),
    );
  }

  static matchListBottomBar(bool visible, int hideNum,
      {VoidCallback? onClose, VoidCallback? onDefault}) {
    return Positioned(
      bottom: 0,
      left: 0,
      right: 0,
      child: Visibility(
        visible: visible,
        child: Container(
          // height: 30,
          width: double.infinity,
          color: Colours.redFEECEC,
          padding: const EdgeInsets.symmetric(vertical: 5),
          child: Row(children: [
            const SizedBox(width: 8),
            GestureDetector(
              onTap: () {
                if (onClose != null) {
                  onClose();
                }
              },
              child: Padding(
                padding: const EdgeInsets.all(2),
                child: Image.asset(
                  Utils.getImgPath('close.png'),
                  width: 14,
                  color: Colours.grey99,
                ),
              ),
            ),
            QkdText('筛选后，合计隐藏$hideNum场比赛',
              fontSize: 12, color: Colours.grey66
            ),
            const Spacer(),
            QkdButton(
              onPressed: () {
                if (onDefault != null) {
                  onDefault();
                }
              },
              height: 22,
              width: 64,
              text: '恢复默认',
              textStyle: const TextStyle(fontSize: 12, fontWeight: FontWeight.normal),
              borderRadius: 11,
              borderSide: BorderSide(color: Colours.main.withOpacity(0.5), width: 0.5),
            ),
            const SizedBox(
              width: 12,
            )
          ]),
        ),
      ),
    );
  }

  // //type 0 纯色  1边框
  // Widget focusButton(int focus, String userId,
  //     {int type = 0,
  //     double width = 56,
  //     double height = 24,
  //     required Function(int?) onTap}) {
  //   Color bgColor = Colours.white;
  //   if (type == 0) {}
  //   return CommonButton(
  //       text: focus == 0 ? '关注' : '已关注',
  //       textStyle: TextStyle(
  //           color: focus == 0 ? Colours.blue2766D6 : Colours.grey99,
  //           fontSize: 13,
  //           fontWeight: FontWeight.w400),
  //       side: focus == 0 && type == 1
  //           ? BorderSide(color: Colours.blue2766D6, width: 0.5)
  //           : BorderSide.none,
  //       radius: 4,
  //       minHeight: height,
  //       minWidth: width,
  //       backgroundColor: focus == 0 ? Colours.white : Colours.greyF2,
  //       onPressed: () async {
  //         if (focus == 0) {
  //           final result = await UserApi.focus(userId: userId, expertId: ex);
  //           if (result == 200) {
  //             ToastUtils.show('关注成功');
  //             onTap(1);
  //             return;
  //           }
  //         } else {
  //           final result = await UserApi.unFocus(userId);
  //           if (result == 200) {
  //             ToastUtils.show('取消关注成功');
  //             onTap(0);
  //             return;
  //           }
  //         }
  //         onTap(null);
  //       });
  // }

  static Widget userAppbarTitle(
      {String? logo,
      String? name,
      int? focus,
      String? userId,
      String? expertId,
      Function(int?)? onTap,
      Color color = Colours.text}) {
    return Row(
      children: [
        ClipOval(
          child: CachedNetworkImage(
            imageUrl: logo ?? '',
            width: color == Colours.white ? 36 : 40,
            height: color == Colours.white ? 36 : 40,
            fit: BoxFit.cover,
          )
        ),
        const SizedBox(width: 8),
        QkdText(
          name ?? '',
          color: color, fontSize: 16,
        ),
        const Spacer(),
        FocusButton(
          type: 0,
          focusColorSetting: color == Colours.text?null:ColorSetting(
            backgroundColor: Colours.white, textColor: Colours.main
          ),
          unFocusColorSetting: color == Colours.text?null:ColorSetting(
            backgroundColor: Colours.greyF2, textColor: Colours.grey99
          ),
          focus ?? 0,
          userId: userId,
          expertId: expertId,
          onTap: onTap,
        ),
        const SizedBox(width: 16)
      ],
    );
  }

  static BallHeader refreshHeader(
      {IndicatorPosition position = IndicatorPosition.above,
      bool clamping = false,Color? backgroundColor}) {
    return BallHeader(
      clamping: clamping,
      position: position,
      triggerOffset: 106,
      backgroundColor: backgroundColor
    );
    // return ClassicHeader(
    //   clamping: clamping,
    //   position: position,
    //   dragText: "下拉刷新",
    //   armedText: "松开刷新",
    //   failedText: "刷新失败",
    //   readyText: "正在刷新...",
    //   processingText: "正在刷新...",
    //   processedText: "刷新完成",
    //   messageText: '上次更新时间 %T',
    //   showMessage: false,
    //   iconTheme: IconThemeData(color: Colours.text),
    //   textStyle: TextStyle(fontSize: 12, color: Colours.grey99),
    //   messageStyle: TextStyle(fontSize: 10, color: Colours.grey99),
    //   // progressIndicatorSize: 32.0
    // );
  }

  static ClassicHeader refreshHeader1(
      {IndicatorPosition position = IndicatorPosition.above,
        bool clamping = false}) {
    return ClassicHeader(
      clamping: clamping,
      position: position,
      dragText: "下拉刷新",
      armedText: "松开刷新",
      failedText: "刷新失败",
      readyText: "正在刷新...",
      processingText: "正在刷新...",
      processedText: "刷新完成",
      messageText: '上次更新时间 %T',
      showMessage: false,
      iconTheme: const IconThemeData(color: Colours.text),
      textStyle: const TextStyle(fontSize: 12, color: Colours.grey99),
      messageStyle: const TextStyle(fontSize: 10, color: Colours.grey99),
      // progressIndicatorSize: 32.0
    );
  }

  static ClassicFooter refreshFooter(
      {IndicatorPosition position = IndicatorPosition.above,
      String? readyText,
      String? noMoreText,
      bool? clamping}) {
    return ClassicFooter(
      dragText: "上拉加载",
      armedText: "松开刷新",
      failedText: "加载失败",
      readyText: readyText ?? "正在加载更多的数据...",
      processingText: readyText ?? "正在加载更多的数据...",
      processedText: "加载完成",
      noMoreText: noMoreText ?? "没有更多内容了~",
      messageText: '上次更新时间 %T',
      noMoreIcon: Container(),
      failedIcon: Container(),
      succeededIcon: Container(),
      iconDimension: 0,
      spacing: 0,
      showMessage: false,
      textStyle: const TextStyle(fontSize: 12, color: Colours.grey99),
      messageStyle: const TextStyle(fontSize: 10, color: Colours.grey99),
      position: position,
      // progressIndicatorSize: 32.0
    );
  }
}
